package com.demo.auth.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import com.demo.auth.common.service.MobileService;
import com.demo.auth.common.service.TokenService;
import com.demo.auth.domain.LoginForm;
import com.demo.common.core.result.R;
import com.demo.common.core.utils.format.FormatUtils;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * @author MyPC
 */
@Api(tags = "B - 手机号登录")
@RestController
@RequestMapping("/mobile")
public class MobileController {

    @Autowired
    private MobileService mobileService;

    @Autowired
    private TokenService tokenService;

    @GetMapping("/getMobileCode")
    @ApiOperation("获取短信验证码")
    public R<Map<String, String>> getLoginCode(@ApiParam("手机号") @RequestParam String mobile) {
        if (!FormatUtils.isMobile(mobile)) {
            return R.fail("手机号格式不正确");
        }
        return R.ok(mobileService.getCode(mobile));
    }

    @PostMapping("/mobileLogin")
    @ApiOperation("短信验证码登录")
    public R<SaTokenInfo> getCode(@Valid @RequestBody LoginForm loginForm) {
        return R.ok(tokenService.createToken(mobileService.getLoginUser(loginForm)));
    }
}
